package cn.zjutleo.village_server.service;

import cn.hutool.core.date.DateTime;
import cn.zjutleo.village_server.dto.CooperateDto;
import cn.zjutleo.village_server.dto.CooperateSelectDto;
import cn.zjutleo.village_server.dto.CooperateCheckDto;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.mapper.CooperateMapper;
import cn.zjutleo.village_server.vo.CooperateVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author ：杜雨茜
 * @Description ：村民互助活动
 * @Date ：2021/12/19 15:53
 **/
@Service
public class CooperateService {
    @Resource
    CooperateMapper cooperateMapper;

    /**
     * Description 根据id查找活动，找不到则返回报错
     **/
    public CooperateVo getExistCooperateById(Integer cooperateId) throws SelectException {
        CooperateVo cooperateVo = cooperateMapper.selectByPrimaryKey(cooperateId);
        if (cooperateVo == null) {
            try {
                throw new SelectException("活动不存在！");
            } catch (SelectException e) {
                e.printStackTrace();
            }
        }
        return cooperateVo;
    }
    /**
     * Description 保存
     **/
    public void saveCooperate(CooperateVo cooperateVo) {
        cooperateMapper.insertSelective(cooperateVo);
    }

    /**
     * Description 删除
     **/
    public void deleteCooperateById(Integer id) {
        cooperateMapper.deleteByPrimaryKey(id);
    }

    /**
     * Description 更新，根据id更新活动类型和活动进行状态
     **/

    public void updateCooperateById(CooperateDto cooperateDto) {
        CooperateVo  cooperateVo= new CooperateVo();
        BeanUtils.copyProperties(cooperateDto, cooperateVo);
        cooperateMapper.updateByPrimaryKeySelective(cooperateVo);
    }

    /**
     * Description 更新，根据id更新活动审核状态
     **/

    public void updateCooperateByCheck(CooperateCheckDto cooperateCheckDto) {
        cooperateMapper.updateByPrimaryKeyCheck(cooperateCheckDto);
    }

    public List<CooperateDto> selectCooperateList(CooperateSelectDto cooperateSelectDto) {
        List<CooperateDto> result = cooperateMapper.selectCooperateList(cooperateSelectDto);
        return result;
    }

    /**
     * Description 分页
     **/
    public PageInfo<CooperateDto> selectCooperatePage(CooperateSelectDto cooperateSelectDto, Integer start, Integer limit) {
        PageHelper.startPage(start, limit);
        Page<CooperateDto> tmp = cooperateMapper.selectCooperatePage(cooperateSelectDto);
        PageInfo<CooperateDto> result=new PageInfo<>(tmp);
        return result;
    }

    /**
     * @author: 杜雨茜
     * @description: 获取报名的开始时间
     * @param cooperateId 活动id
     * @return
     **/
    public Date getsignStartDateTime(int cooperateId){
        CooperateVo cooperateVo = cooperateMapper.selectByPrimaryKey(cooperateId);
        return cooperateVo.getSignstartdatetime();
    }
    /**
     * @author: 杜雨茜
     * @description: 获取报名的结束时间
     * @param cooperateId 活动id
     * @return
     **/
    public Date getsignEndDateTime(int cooperateId){
        CooperateVo cooperateVo = cooperateMapper.selectByPrimaryKey(cooperateId);
        return cooperateVo.getSignenddatetime();
    }

}
